# Copyright (C) 2002-2004, 2006, 2009 The Written Word, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# $Id: xmlplain.py 694 2010-02-19 14:55:45Z china $

import os
import sys


import prog
import sbdb

from prog.archive import Archive
from xmlutil import XMLSyntaxError, objectify

class Archive (Archive):
  def unpack (self, dry_run, verbose, callback, dest = None,
              filelist = None, list_files = False):
    # extract all sources files if at least one does not exist.
    # we could extract source files one at a time but this
    # consumes time skipping over the un-processed XML source
    # entries.
    try:
      xml = objectify.parse (self.path)
    except XMLSyntaxError, msg:
      raise XMLParseError ('Syntax error while parsing ' +
                           os.path.normpath (self.path) +
                           ', the following error was encountered: ' +
                           str (msg))
    
    SbDBEntry (xml, [self.module], dest, filelist, callback)

  def open (self, member, mode):
    # XML archives contain no 'members' like zip archives
    return open (self.path, mode)
